﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Xml.Serialization;
using ComLib.ConfigService;
using ComLib.ComFun;

namespace ScheduleAutoAction
{
    /// <summary>
    /// 执行频率
    /// </summary>
    [Serializable]
    public enum ExecutionInterval : int
    {
        /// <summary>
        /// 一天一次
        /// </summary>
        DayOnce = 0,
        /// <summary>
        /// 每周一次
        /// </summary>
        WeekOnce = 1,
        /// <summary>
        /// 每个月一次
        /// </summary>
        MonthOnce = 2,
        /// <summary>
        /// 启动应用程序时
        /// </summary>
        ApplicationStart = 3,
        /// <summary>
        /// 退出应用程序时
        /// </summary>
        ApplicationExit = 4,
    }

    /// <summary>
    /// 每周的执行日期
    /// </summary>
    [Serializable]
    public enum ExecutionWeekDay : int
    {      
        //
        // 摘要: 
        //     表示星期一。
        Monday = 0,
        //
        // 摘要: 
        //     表示星期二。
        Tuesday = 1,
        //
        // 摘要: 
        //     表示星期三。
        Wednesday = 2,
        //
        // 摘要: 
        //     表示星期四。
        Thursday = 3,
        //
        // 摘要: 
        //     表示星期五。
        Friday = 4,
        //
        // 摘要: 
        //     表示星期六。
        Saturday = 5,
        // 摘要: 
        //     表示星期日。
        Sunday = 6,
    }

    /// <summary>
    /// 距离今天的天数
    /// </summary>
    [Serializable]
    public enum DaysFormToday : int
    {
        /// <summary>
        /// 一天
        /// </summary>
        OneDay = 0,
        /// <summary>
        /// 三天
        /// </summary>
        ThreeDays = 1,
        /// <summary>
        /// 一周
        /// </summary>
        OneWeek = 2,
        /// <summary>
        /// 10天
        /// </summary>
        TenDays = 3,
        /// <summary>
        /// 一个月
        /// </summary>
        OneMonth = 4,
    }

    /// <summary>
    /// 自动任务计划选项
    /// </summary>
    [XmlRootAttribute("ScheduleCleaningOption", Namespace = "ScheduleAutoAction", IsNullable = false)]
    public class ScheduleCleaningOption : XmlSerializeBase
    {
        /// <summary>
        /// 是否开启自动清理任务计划功能
        /// </summary>
        private bool _enabled = false;
        public bool Enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }

        /// <summary>
        /// 是否仅仅在大于临界值时执行清理
        /// </summary>
        private bool _bOnlyExecuteWhenOverCV = false;
        public bool OnlyExecuteWhenOverCV
        {
            get { return _bOnlyExecuteWhenOverCV; }
            set { _bOnlyExecuteWhenOverCV = value; }
        }

        /// <summary>
        /// 内存占用临界值
        /// </summary>
        private int _nCriticalMemorySize = 3;
        public int CriticalMemorySize
        {
            get { return _nCriticalMemorySize; }
            set { _nCriticalMemorySize = value; }
        }

        /// <summary>
        /// 计划任务执行的频率
        /// </summary>
        private ExecutionInterval _intervalTime = ExecutionInterval.MonthOnce;
        public ExecutionInterval IntervalTime
        {
            get { return _intervalTime; }
            set { _intervalTime = value; }
        }

        /// <summary>
        /// 计划任务执行工作日
        /// </summary>
        private ExecutionWeekDay _executeWeekDay = ExecutionWeekDay.Wednesday;
        public ExecutionWeekDay ExecuteWeekDay
        {
            get { return _executeWeekDay; }
            set { _executeWeekDay = value; }
        }

        /// <summary>
        /// 任务执行时间
        /// </summary>
        private DateTime _dtExecute = new DateTime(2017, 10, 10, 9, 0, 0);
        public DateTime ExecuteTime
        {
            get { return _dtExecute; }
            set { _dtExecute = value; }
        }

        /// <summary>
        /// 被清理目标历史文件距离今天的间隔时间
        /// </summary>
        private DaysFormToday _nDaysFormToday = DaysFormToday.TenDays;
        public DaysFormToday HistoryFilesFormToday
        {
            get { return _nDaysFormToday; }
            set { _nDaysFormToday = value; }
        }

        /// <summary>
        /// 是否静默执行任务计划(神不知鬼不觉的)
        /// </summary>
        private bool _bSilence = false;
        public bool ExecuteWithSilence
        {
            get { return _bSilence; }
            set { _bSilence = value; }
        }

        /// <summary>
        /// 执行任务时是否询问
        /// </summary>
        private bool _bEnquiring = false;
        public bool Enquiring
        {
            get { return _bEnquiring; }
            set { _bEnquiring = value; }
        }

        public ScheduleCleaningOption()
        {
        }

        public int GetTimespan()
        {
            switch (_nDaysFormToday)
            {
                case DaysFormToday.OneDay:
                    return 1;
                case DaysFormToday.ThreeDays:
                    return 3;
                case DaysFormToday.OneWeek:
                     return 7;
                case DaysFormToday.TenDays:
                     return 10;
                case DaysFormToday.OneMonth:
                     return 30;
                default:
                     return 10;
            }
        }

        public override bool Serialize()
        {
            return Serializer.SaveToXml(ConfigFilePath, this);
        }

       public override bool Load()
        {
            ScheduleCleaningOption obj = (ScheduleCleaningOption)Serializer.LoadFromXml(ConfigFilePath, this.GetType());
            if (obj != null)
            {
                this._bEnquiring = obj._bEnquiring;
                this._bOnlyExecuteWhenOverCV = obj._bOnlyExecuteWhenOverCV;
                this._nVer = obj._nVer;
                this._bSilence = obj._bSilence;
                this._dtExecute = obj._dtExecute;
                this._enabled = obj._enabled;
                this._error = obj._error;
                this._executeWeekDay = obj._executeWeekDay;
                this._intervalTime = obj._intervalTime;
                this._nCriticalMemorySize = obj._nCriticalMemorySize;
                this._nDaysFormToday = obj._nDaysFormToday;
                return true;
            }
            else
            {
                ErrorMessage = Serializer.Message;
                return false;
            }
        }

        public bool NeedStartupAtAppStart()
        {
            if(!_enabled)
            {
                return false;
            }
            switch (_intervalTime)
            {
                case ExecutionInterval.DayOnce:
                case ExecutionInterval.WeekOnce:
                case ExecutionInterval.MonthOnce:
                    return true;
                case ExecutionInterval.ApplicationStart:
                case ExecutionInterval.ApplicationExit:
                default:
                    return false;
            }
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("自动清理任务计划选项: ");
            builder.Append("[是否启用]:" + (_enabled ? "是" : "否"));
            builder.Append("; [是否静默执行]:" + (_bSilence ? "是" : "否"));
            builder.Append("; [执行任务时是否询问]:" + (_bEnquiring ? "是" : "否"));
            builder.Append("; [是否仅仅在大于临界值时执行清理]:" + (_bOnlyExecuteWhenOverCV ? "是" : "否"));
            builder.Append("; [内存占用临界值]:" + _nCriticalMemorySize.ToString() + "GB");
            builder.Append("; [计划任务执行间隔]:" + _intervalTime.ToString());
            builder.Append("; [计划任务执行时间]:" + _dtExecute.ToString());
            builder.Append("; [计划任务执行工作日]:" + _executeWeekDay.ToString());
            builder.Append("; [被清理目标历史文件距离今天的间隔时间]:" + _nDaysFormToday.ToString());
            return builder.ToString();
        }
    }
}
